﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserManagement.ascx.cs"
    Inherits="Admin_UserControls_UserManagement" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<style type="text/css">
    .menuleft_small
    {
        width: 225px;
    }
    .menuleft_lagre
    {
        width: 300px;
    }
    .errorMessage
    {
        color: Red;
    }
    .button
    {
        width: 85px;
    }
</style>

<script type="text/javascript">

    function Validator(ControlClick)
    {   var message = new Array();
        var ControlID = new Array();
        var ControlDislayMessage = new Array();
        
        message[0] = 'Không được bỏ trống';
        message[1] = 'Không được bỏ trống';
        message[2] = 'Không được bỏ trống';
        message[3] = 'Không được bỏ trống';
        message[4] = 'Không được bỏ trống';
        message[5] = 'Không được bỏ trống';
        
        ControlID[0] = document.getElementById('<%=TextBox_UserName.ClientID%>');
        ControlID[1] = document.getElementById('<%=TextBox_Password.ClientID%>');
        ControlID[2] = document.getElementById('<%=TextBox_ConfirmPassword.ClientID%>');
        ControlID[3] = document.getElementById('<%=TextBox_FirstName.ClientID%>');
        ControlID[4] = document.getElementById('<%=TextBox_LastName.ClientID%>');
        ControlID[5] = document.getElementById('<%=TextBox_Email.ClientID%>');
                
        ControlDislayMessage[0] = document.getElementById('<%=Label_CheckUserName.ClientID%>');
        ControlDislayMessage[1] = document.getElementById('<%=Label_CheckPassword.ClientID%>');
        ControlDislayMessage[2] = document.getElementById('<%=Label_CheckConfirmPassword.ClientID%>');
        ControlDislayMessage[3] = document.getElementById('<%=Label_CheckFirstName.ClientID%>');
        ControlDislayMessage[4] = document.getElementById('<%=Label_CheckLastName.ClientID%>');
        ControlDislayMessage[5] = document.getElementById('<%=Label_CheckEmail.ClientID%>');
        var Count = 0;
        var Error1 = true;
        var Error2 = true;
        var Error3 = true;
        var Error4 = true;
        var Content;
        while (Count < ControlClick)
        {
            if(ControlID[Count].value.length == 0)
            {
                ControlDislayMessage[Count].innerHTML = message[Count];
                if(Error1)
                {
                    ControlID[Count].focus();
                    Error1 = false;
                }
            }
            else
            {
                ControlDislayMessage[Count].innerHTML = null;
            }
            ++Count;
        }
        Error2 = CheckUserName(ControlID[0].value);
        if(ControlClick > 2)
            Error3 = CheckConfirmPassword();
        if(ControlClick > 5)
            Error4 = ValidateEmail();
        var Error = Error1 && Error2 && Error3 && Error4;
        return Error;
    }
    
    function CheckConfirmPassword()
    {
        var Password = document.getElementById('<%=TextBox_Password.ClientID%>');
        var ConfirmPassword = document.getElementById('<%=TextBox_ConfirmPassword.ClientID%>');
        if(Password.value.length > 0)
        {
            if(Password.value != ConfirmPassword.value)
            {
                document.getElementById('<%=Label_CheckConfirmPassword.ClientID%>').innerHTML = 'Xác nhận mật khẩu không khớp';
                ConfirmPassword.focus();
                return false;
            }
            else
            {
                document.getElementById('<%=Label_CheckConfirmPassword.ClientID%>').innerHTML = 'OK';
                return true;
            }
        }
    }
    
    function ValidateEmail()
    {        
        var address = document.getElementById('<%=TextBox_Email.ClientID%>').value;
        if(address.length > 0)
        {
            var atpos = address.indexOf("@");
            var dotpos = address.lastIndexOf(".");
            var space = address.indexOf(" "); 
            if (atpos < 1 || dotpos < (atpos + 2) || (dotpos + 2) >= address.length || space != -1)
            {
                document.getElementById('<%=Label_CheckEmail.ClientID%>').innerHTML = 'Địa chỉ Email không hợp lệ';
                document.getElementById('<%=TextBox_Email.ClientID%>').focus();
                return false;
            }
            else
            {
                document.getElementById('<%=Label_CheckEmail.ClientID%>').innerHTML = 'OK';
                return true;
            }
        }
    }
    
    function CheckUserName(UserName)
    {
        if(UserName.length > 0)
        {
            if(UserName.indexOf(" ") != -1 || isNaN(UserName.substring(0,1)) == false)
            {
                document.getElementById('<%=Label_CheckUserName.ClientID%>').innerHTML = 'Tên không hợp lệ';
                return false;
            }
            return true;
        }
    }
    
</script>

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<div class="title">
    <h5>
        QUẢN LÝ NGƯỜI DÙNG
    </h5>
</div>
<div class="table">
    <asp:Panel ID="Panel1" runat="server">
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="True" GridLines="None"
            AllowPaging="True" OnPreRender="RadGrid1_PreRender" OnItemCommand="RadGrid1_iTemcommand"
            AutoGenerateColumns="False" Skin="Office2007">
            <MasterTableView CommandItemDisplay="TopAndBottom" NoMasterRecordsText="Không có kết quả">
                <CommandItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl="~/Files/images/add.gif" />
                    <asp:LinkButton ID="LinkButton_Add" runat="server" CommandName="Them" AccessKey="N">Thêm người dùng</asp:LinkButton>
                </CommandItemTemplate>
                <RowIndicatorColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridBoundColumn DataField="user_id" DataType="System.Int32" HeaderText="UserID"
                        UniqueName="user_id" Visible="false">
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn Text="Xem" UniqueName="Xem" CommandName="Xem" ImageUrl="~/Files/images/view.gif"
                        HeaderImageUrl="~/Files/images/view.gif">
                    </telerik:GridButtonColumn>
                    <telerik:GridBoundColumn DataField="username" HeaderText="Tên tài khoản" UniqueName="username">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="name_first" HeaderText="Họ" UniqueName="name_first">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="name_last" HeaderText="Tên" UniqueName="name_last">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="status" HeaderText="Trạng thái" UniqueName="status">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="created_date" HeaderText="Ngày tạo" UniqueName="created_date"
                        DataType="System.DateTime" DataFormatString="{0:dd/MM/yyyy}">
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn Text="Sửa" UniqueName="Sua" CommandName="Sua" ImageUrl="~/Files/images/edit.gif"
                        HeaderImageUrl="~/Files/images/edit.gif">
                    </telerik:GridButtonColumn>
                    <telerik:GridButtonColumn Text="Chặn người dùng" UniqueName="Xoa" CommandName="Xoa"
                        ConfirmText="Bạn có chắc chắn muốn chặn?" ImageUrl="~/Files/images/delete.gif"
                        HeaderImageUrl="~/Files/images/delete.gif">
                    </telerik:GridButtonColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings>
                <Selecting AllowRowSelect="True" />
            </ClientSettings>
        </telerik:RadGrid>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server">
        <table style="width: 100%; margin-top: 25px;">
            <tr>
                <td style="width: 20%">
                    Tên tài khoản
                </td>
                <td style="width: 80%">
                    <asp:TextBox ID="TextBox_UserName" runat="server" MaxLength="30" CssClass="menuleft_small"></asp:TextBox>
                    *
                    <asp:Label ID="Label_CheckUserName" runat="server" CssClass="errorMessage" Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Mật khẩu:
                </td>
                <td>
                    <asp:TextBox ID="TextBox_Password" onfocus='javascript:return Validator(1)' runat="server"
                        MaxLength="30" CssClass="menuleft_small" TextMode="Password"></asp:TextBox>
                    *
                    <asp:Label ID="Label_CheckPassword" runat="server" CssClass="errorMessage" Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Xác nhận mật khẩu:
                </td>
                <td>
                    <asp:TextBox ID="TextBox_ConfirmPassword" onfocus='javascript:return Validator(2)'
                        onchange='javascript:return CheckConfirmPassword()' runat="server" MaxLength="30"
                        CssClass="menuleft_small" TextMode="Password"></asp:TextBox>
                    *
                    <asp:Label ID="Label_CheckConfirmPassword" runat="server" CssClass="errorMessage"
                        Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Họ:
                </td>
                <td>
                    <asp:TextBox ID="TextBox_FirstName" runat="server" onfocus='javascript:return Validator(3)'
                        MaxLength="30" CssClass="menuleft_small"></asp:TextBox>
                    *
                    <asp:Label ID="Label_CheckFirstName" runat="server" CssClass="errorMessage" Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Tên:
                </td>
                <td>
                    <asp:TextBox ID="TextBox_LastName" runat="server" onfocus='javascript:return Validator(4)'
                        MaxLength="30" CssClass="menuleft_small"></asp:TextBox>
                    *
                    <asp:Label ID="Label_CheckLastName" runat="server" CssClass="errorMessage" Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Địa chỉ email:
                </td>
                <td>
                    <asp:TextBox ID="TextBox_Email" onfocus='javascript:return Validator(5)' onchange='javascript:return ValidateEmail()'
                        runat="server" MaxLength="50" CssClass="menuleft_small"></asp:TextBox>
                    *
                    <asp:Label ID="Label_CheckEmail" runat="server" CssClass="errorMessage" Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Trạng thái:
                </td>
                <td>
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" Width="150px">
                        <asp:ListItem Selected="True" Value="0">Chưa kích hoạt</asp:ListItem>
                        <asp:ListItem Value="1">Đã kích hoạt</asp:ListItem>
                        <asp:ListItem Value="2">Tạm ngừng</asp:ListItem>
                        <asp:ListItem Value="3">Đã bị chặn</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Label ID="Label_Result" runat="server" ForeColor="Red"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="Button_OK" runat="server" CssClass="button" Text="OK" Width="85px" OnClientClick="return Validator(6)"
                        OnClick="Button_OK_Click" />
                </td>
            </tr>
        </table>
    </asp:Panel>
</div>
